using SCore.Editor.BuildPackage.Addressable;
using Function.SerializedPropertyAttribute;
using SCore.Util;
using UnityEditor;
using UnityEngine;

namespace SCore.Editor.BuildPackage
{
    [CustomEditor(typeof(BuildTarget))]
    public class BuildTargetEditor : ObjectDrawEditor<BuildTarget>
    {
    }

    [CreateAssetMenu(fileName = "SBuildData", menuName = "ScriptableObject/BuildPipelineConfig/SBuildTargetData",
        order = 1)]
    public class BuildTarget : SingleScriptableObject<BuildTarget>
    {
        [Foldout("基础数据", true)] [Head("构建类型")] [DefinedValues(nameof(GetTargetTypeNames))]
        public string _targetType;

        [Head("构建设置")] public BuildOptions _buildOptions;

        [Head("addressable _profileName")] public string _profileName = "dev";

        public SerializableDictionary<string, string> _defines = new SerializableDictionary<string, string>();

        private string[] GetTargetTypeNames()
        {
            return new[]
            {
                BuildPipelineUtils.STargetType.Develop,
                BuildPipelineUtils.STargetType.Release,
            };
        }

        private void SetDefines(BuildTargetGroup buildTargetGroup)
        {
            var defines = BuildPipelineUtils.GetScriptingDefineSymbols(buildTargetGroup);
            //log
            var logDefines = _defines["Log"];
            if (BuildPipelineConfig.Instance._withDebugLog)
            {
                defines.Add(logDefines);
            }
            else
            {
                if (defines.Contains(logDefines))
                {
                    defines.Remove(logDefines);
                }
            }

            var cgGlobal = _defines["cgGlobal"];
            var cgCN = _defines["cgCN"];
            if (BuildPipelineConfig.Instance._isCGSDKGlobal)
            {
                defines.Add(cgGlobal);

                if (defines.Contains(cgCN))
                {
                    defines.Remove(cgCN);
                }
            }
            else
            {
                defines.Add(cgCN);
                if (defines.Contains(cgGlobal))
                {
                    defines.Remove(cgGlobal);
                }
            }


            BuildPipelineUtils.SetScriptingDefineSymbols(buildTargetGroup, defines);
        }

        [ButtonMethod]
        public void SBuild()
        {
            $"[BuildPackage] {_targetType} Start_____________________".Log();
            if (_targetType == BuildPipelineUtils.STargetType.Develop)
            {
                EditorUserBuildSettings.development = true;
                EditorUserBuildSettings.allowDebugging = true;
                EditorUserBuildSettings.connectProfiler = true;
                // EditorUserBuildSettings.androidCreateSymbolsZip = true;
                EditorUserBuildSettings.buildAppBundle = false;
                EditorUserBuildSettings.buildWithDeepProfilingSupport = true;
            }
            else
            {
                EditorUserBuildSettings.development = false;
                EditorUserBuildSettings.allowDebugging = false;
                EditorUserBuildSettings.connectProfiler = false;
                EditorUserBuildSettings.buildAppBundle = BuildPipelineConfig.Instance._isExportAAb;
            }

            BuildAddressable.SetProfile(_profileName);
            var buildTargetGroup = BuildPipelineConfig.Instance._buildChannel.GetBuildTargetGroup();
            SetDefines(buildTargetGroup);

            $"[BuildPackage] {_targetType} End_____________________".Log();
        }
    }
}